package com.a918.controller;
/**
 * 这个页面主要接前台的请求，处理前台事务
 */

import com.a918.dao.FrontUnionMapper;
import com.a918.dto.ContentDto;
import com.a918.dto.FrontUnionDto;
import com.a918.entity.*;
import com.a918.service.FrontService;
import com.a918.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/front")
public class FrontController {

    private FrontService frontService;
    private UserService userService;

    @Autowired
    public FrontController(FrontService frontService, UserService userService) {
        this.frontService = frontService;
        this.userService = userService;
    }


    @GetMapping(value = {"", "/index"})
    public String front_index(Model model,HttpServletRequest request) {
        model.addAttribute("head_title", "全国工业博物馆联盟-首页");
        //取图片
        List<Cover> allCover = frontService.getAllCover();
        model.addAttribute("coverlist",allCover);
        //会员风采
        PageInfo<FrontUnionDto> unionList=frontService.getCheckedUnion();
        model.addAttribute("unionlist",unionList.getList());
        //联盟动态部分
        Map<String,Object> xwMap = new HashMap<>();
        xwMap.put("title",request.getParameter("title"));
        Integer xwid = frontService.gettypeIdByCode("xwdt");
        xwMap.put("typeid",xwid);
        xwMap.put("status","yfb");
        model.addAttribute("contentlistxw", frontService.getCheckedContent(xwMap).getList());
        Map<String,Object> hdMap = new HashMap<>();
        hdMap.put("title",request.getParameter("title"));
        Integer hdid = frontService.gettypeIdByCode("hdzx");
        hdMap.put("typeid",hdid);
        hdMap.put("status","yfb");
        model.addAttribute("contentlisthd", frontService.getCheckedContent(hdMap).getList());
        Map<String,Object> tzMap = new HashMap<>();
        tzMap.put("title",request.getParameter("title"));
        Integer tzid = frontService.gettypeIdByCode("tztg");
        tzMap.put("typeid",tzid);
        tzMap.put("status","yfb");
        model.addAttribute("contentlisttz", frontService.getCheckedContent(tzMap).getList());
        Map<String,Object> xsMap = new HashMap<>();
        xsMap.put("title",request.getParameter("title"));
        Integer xsid = frontService.gettypeIdByCode("xscg");
        xsMap.put("typeid",xsid);
        xsMap.put("status","yfb");
        model.addAttribute("contentlistxs", frontService.getCheckedContent(xsMap).getList());
        return "front/index";
    }

    @GetMapping("/aboutus/{type}")
    public String memberadd(@PathVariable(value = "type", required = true) String type, Model model) {
        switch (type) {
            //关于我们
            case "gywm":
                model.addAttribute("content", frontService.getCommonIntro("gywm"));
                model.addAttribute("title", "关于我们");
                model.addAttribute("head_title", "全国工业博物馆联盟-关于我们");
                break;
            //联盟章程
            case "lmzc":
                model.addAttribute("content", frontService.getCommonIntro("lmzc"));
                model.addAttribute("title", "联盟章程");
                model.addAttribute("head_title", "全国工业博物馆联盟-联盟章程");
                break;
            //联盟架构
            case "lmjg":
                model.addAttribute("content", frontService.getCommonIntro("lmjg"));
                model.addAttribute("title", "联盟架构");
                model.addAttribute("head_title", "全国工业博物馆联盟-联盟架构");
                break;
            //联盟倡议
            case "lmcy":
                model.addAttribute("content", frontService.getCommonIntro("lmcy"));
                model.addAttribute("title", "联盟倡议");
                model.addAttribute("head_title", "全国工业博物馆联盟-联盟倡议");
                break;
        }
        return "front/aboutus";

    }

    @RequestMapping("/newslist*")
    public String memberaddn(HttpServletRequest request,String type, Integer pagenum, Model model) {
        Map<String,Object> searchMap = new HashMap<>();
        if(request.getMethod().equals("POST")){
            searchMap.put("title",request.getParameter("title"));
        }
        Integer typeid = frontService.gettypeIdByCode(type);
        searchMap.put("typeid",typeid);
        searchMap.put("status","yfb");
        PageHelper.startPage(pagenum,10);
        PageInfo<ContentDto> contentList=frontService.getCheckedContent(searchMap);
        model.addAttribute("page",contentList);
        model.addAttribute("type",type);
        model.addAttribute("contentlist", contentList.getList());
        List<Content> recommendList=frontService.getRecommend(searchMap);
        model.addAttribute("recommendlist",recommendList);
        switch(type){
            //新闻动态
            case "xwdt":
                model.addAttribute("title","新闻动态");
                model.addAttribute("head_title","全国工业博物馆联盟-新闻动态");
                break;
            //活动展讯
            case "hdzx":
                model.addAttribute("title","活动展讯");
                model.addAttribute("head_title","全国工业博物馆联盟-活动展讯");
                break;
            //通知通告
            case "tztg":
                model.addAttribute("title","通知通告");
                model.addAttribute("head_title","全国工业博物馆联盟-通知通告");
                break;
            //学术成果
            case "xscg":
                model.addAttribute("title","学术成果");
                model.addAttribute("head_title","全国工业博物馆联盟-学术成果");
                break;
        }
        return "front/newslist";
    }

    @GetMapping("/contentdetail/{id}")
    public String contentdetail(@PathVariable(value = "id", required = true) String id, Model model) {
        Content content = frontService.getContentById(Integer.valueOf(id));
        User user=frontService.getNameById(Integer.valueOf(content.getAdduserid()));
        model.addAttribute("content", content);
        model.addAttribute("user",user);
        frontService.UpdateViewnum(content);
        return "front/news";
    }


    @GetMapping("/usershow*")
    public String memberaddu( String type,Integer pagenum,Model model) {
        model.addAttribute("type",type);
        switch (type) {
            //用户风采
            case "hyfc":
                PageHelper.startPage(pagenum,8);
                PageInfo<FrontUnionDto> unionList=frontService.getCheckedUnion();
                model.addAttribute("page",unionList);
                model.addAttribute("unionlist",unionList.getList());
                model.addAttribute("title", "会员风采");
                model.addAttribute("head_title", "全国工业博物馆联盟-会员风采");
                break;
            //博物馆展厅
            case "bwgzt":
                PageHelper.startPage(pagenum,8);
                List<FrontMember> memberList=frontService.getMemberByStatus("yfb");
                PageInfo<FrontMember> pbw=new PageInfo<FrontMember>(memberList);
                model.addAttribute("page",pbw);
                model.addAttribute("memberlist",memberList);
                model.addAttribute("title", "博物馆展厅");
                model.addAttribute("head_title", "全国工业博物馆联盟-博物馆展厅");
                break;
        }
        return "front/usershow";

    }

    @GetMapping("/vipdetail*")
    public String vipdetail( String id, Model model,String type) {
        model.addAttribute("type",type);
        switch (type){
            //会员风采
            case "hyfc":
                FrontUnion union=frontService.getContextById(Integer.valueOf(id));
                User user=frontService.getNameById(Integer.valueOf(union.getUserid()));
                model.addAttribute("union",union);
                model.addAttribute("user",user);
                break;
            //博物馆展厅
            case "bwgzt":
                FrontMember member=frontService.getIntroductById(Integer.valueOf(id));
                model.addAttribute("member",member);
        }
        return "front/vip";
    }
}
